package entity;

import lombok.Data;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicLong;

@Data
public class WordNode {

    private Map<Character,WordNode> childs;
    private BlockingQueue<String> docIdList;
    private boolean end;
    private String aliasName;
    private String fullName;
    private AtomicLong count = new AtomicLong();

    public WordNode() {
        childs = new HashMap<>();
        end = false;
        docIdList = new ArrayBlockingQueue<>(5);
    }

    public void end(){
        end = true;
    }

    public Set<Character> getChildsSet(){
        return childs.keySet();
    }

    public void incrCount(){
        count.incrementAndGet();
    }

    public void incrIfEnd() {
        if(end){
            incrCount();
        }
    }

    public void add(String docId){
        docIdList.offer(docId);
    }

    public String toOneLine(){
        StringBuilder sb = new StringBuilder();
        for (int i = 5; i > 0; i--) {
            String docId = docIdList.poll();
            if(docId != null){
                sb.append(docId).append(",");
            }else {
                sb.append("  ,");
            }
        }
        return String.format("%-25s\t,%-6s\t,%-8s,%s\n",fullName,aliasName,count,sb.toString());
    }
}
